/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.plugins.impl;

import java.util.List;

import org.grayrabbit.cms.dao.plugins.VoteRecordDao;
import org.grayrabbit.cms.entity.VoteRecord;
import org.grayrabbit.cms.service.plugins.VoteRecordService;
import org.grayrabbit.cms.vo.plugins.VoteRecordBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 16:08:28  
 */
@Service
@Transactional
public class VoteRecordServiceImpl extends GenericServiceImpl implements VoteRecordService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("voteRecordDaoImpl")
	private VoteRecordDao voteRecordDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see VoteRecordService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public VoteRecord getVoteRecord(final String voteRecordId){
		VoteRecord voteRecord = voteRecordDao.findById(voteRecordId);
		voteRecordDao.initialize(voteRecord);
		return voteRecord;
	}
	
	/**
	 * @see VoteRecordService#delete(String)
	 */
	@Transactional
	public void delete(final VoteRecord voteRecord) {
		voteRecordDao.makeTransient(voteRecord);
	}
	
	/**
	 * @see VoteRecordService#save(OperLog)
	 */
	@Transactional
	public void save(final VoteRecord voteRecord) {
		voteRecordDao.makePersistent(voteRecord);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see VoteRecordService#getPage(VoteRecordBean)
	 */
	@Transactional(readOnly=true)
	public Page<VoteRecord> getPage(final VoteRecordBean voteRecordBean){
		Page<VoteRecord> page =  new Page<VoteRecord>();
		page.setTotal(voteRecordDao.count(voteRecordBean));
		page.setResult(voteRecordDao.findByQc(voteRecordDao.assembly(voteRecordBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see VoteRecordService#list(Page, VoteRecordBean)
	 */
	public void list(Page<VoteRecord> page, VoteRecordBean voteRecordBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(voteRecordDao.count(voteRecordBean));
		}
		page.setResult(voteRecordDao.findByPage(page,voteRecordBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see VoteRecordService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			voteRecordDao.makeTransient(voteRecordDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				voteRecordDao.clear();
		}
		
	}
	/**
	 * 得到投票数
	 * @return
	 */
	public List<VoteRecord> findByVoteId(String voteId){
		return voteRecordDao.findByVoteId(voteId);
	}
	
	/**
	 * 得到个投票项投票数
	 * @return
	 */
	public List<VoteRecord> findByOptId(String optId){
		return voteRecordDao.findByOptId(optId);
	}
	
}